package wxk.interview.serializable;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * 使用serializable接口序列化
 * 若字段声明为transient,则该字段不能被序列化,否则你增加方法 writeObject(ObjectOutputStream out)和readObject(ObjectInputStream in),并且为private
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/9.
 * @since 2.2
 */
public class Person implements Serializable {

    private static final long serialVersionUID = 9015806208765982834L;

    private String idNo;
    private String name;
    private transient Integer age;

    public Person(String idNo, String name, Integer age) {
        this.idNo = idNo;
        this.name = name;
        this.age = age;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    private void writeObject(ObjectOutputStream out) throws IOException{
        out.defaultWriteObject();
        out.writeInt(age);
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        age = in.readInt();
    }

    @Override
    public String toString() {
        return "Person{" +
                "idNo='" + idNo + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
